package com.ruoyi.project.system.systematics.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.systematics.domain.GoodsSystematics;
import com.ruoyi.project.system.systematics.service.IGoodsSystematicsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 商品分类管理Controller
 * 
 * @author ruoyi
 * @date 2021-01-17
 */
@Controller
@RequestMapping("/system/systematics")
public class GoodsSystematicsController extends BaseController
{
    private String prefix = "system/systematics";

    @Autowired
    private IGoodsSystematicsService goodsSystematicsService;

    @RequiresPermissions("system:systematics:view")
    @GetMapping()
    public String systematics()
    {
        return prefix + "/systematics";
    }

    /**
     * 查询商品分类管理树列表
     */
    @RequiresPermissions("system:systematics:list")
    @PostMapping("/list")
    @ResponseBody
    public List<GoodsSystematics> list(GoodsSystematics goodsSystematics)
    {
        List<GoodsSystematics> list = goodsSystematicsService.selectGoodsSystematicsList(goodsSystematics);
        return list;
    }

    /**
     * 导出商品分类管理列表
     */
    @RequiresPermissions("system:systematics:export")
    @Log(title = "商品分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsSystematics goodsSystematics)
    {
        List<GoodsSystematics> list = goodsSystematicsService.selectGoodsSystematicsList(goodsSystematics);
        ExcelUtil<GoodsSystematics> util = new ExcelUtil<GoodsSystematics>(GoodsSystematics.class);
        return util.exportExcel(list, "systematics");
    }

    /**
     * 新增商品分类管理
     */
    @GetMapping(value = { "/add/{systematicsId}", "/add/" })
    public String add(@PathVariable(value = "systematicsId", required = false) Long systematicsId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(systematicsId))
        {
            mmap.put("goodsSystematics", goodsSystematicsService.selectGoodsSystematicsById(systematicsId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类管理
     */
    @RequiresPermissions("system:systematics:add")
    @Log(title = "商品分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsSystematics goodsSystematics)
    {
        return toAjax(goodsSystematicsService.insertGoodsSystematics(goodsSystematics));
    }

    /**
     * 修改商品分类管理
     */
    @GetMapping("/edit/{systematicsId}")
    public String edit(@PathVariable("systematicsId") Long systematicsId, ModelMap mmap)
    {
        GoodsSystematics goodsSystematics = goodsSystematicsService.selectGoodsSystematicsById(systematicsId);
        mmap.put("goodsSystematics", goodsSystematics);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类管理
     */
    @RequiresPermissions("system:systematics:edit")
    @Log(title = "商品分类管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsSystematics goodsSystematics)
    {
        return toAjax(goodsSystematicsService.updateGoodsSystematics(goodsSystematics));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:systematics:remove")
    @Log(title = "商品分类管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{systematicsId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("systematicsId") Long systematicsId)
    {
        return toAjax(goodsSystematicsService.deleteGoodsSystematicsById(systematicsId));
    }

    /**
     * 选择商品分类管理树
     */
    @GetMapping(value = { "/selectSystematicsTree/{systematicsId}", "/selectSystematicsTree/" })
    public String selectSystematicsTree(@PathVariable(value = "systematicsId", required = false) Long systematicsId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(systematicsId))
        {
            mmap.put("goodsSystematics", goodsSystematicsService.selectGoodsSystematicsById(systematicsId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载商品分类管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = goodsSystematicsService.selectGoodsSystematicsTree();
        return ztrees;
    }
}
